#!/bin/sh
awk ' BEGIN { 
              FS = " "
              inlpm="false"
	      in_cell="false"
              line_count=1
	      lpm_cell_count=1
	      lpm_instance=1
              lpm_counter_count=1
	      lpm_counter_instance=1
              lpm_ram_dq_count=1
	      lpm_ram_dq_instance=1
	      add_cnt_dir="true"
	      add_cnt_en="false"
            }
/\(cell / { 
            if (inlpm == "true")
            {
               for (i=1;i <= line_count;i++)
               {
		  split(line[i],tok," ")
                  if(tok[1] == "(property")
 		  {	
                     if ((tok[2]=="PVALUE")||(tok[2]=="SVALUE")||\
                         (tok[2]=="NUMWORDS")||(tok[2]=="AD_WIDTH")||\
                         (tok[2]=="INWIDTH")||(tok[2] =="OUTWIDTH")||\
			 (tok[2]=="DISTWIDTHDIST")||\
                         (tok[2]=="SIZE")||(tok[2]=="WIDTH")||\
			 (tok[2]=="WIDTHS")||(tok[2]=="WIDTHA")||\
			 (tok[2]=="WIDTHB")||(tok[2]=="WIDTHP")||\
                         (tok[2]=="DECODES")||(tok[2]=="MODULUS")||\
			 (tok[2]=="AVALUE")||(tok[2]=="CVALUE"))
                     {
                        tok[3]="(integer" 
                        split(tok[4],x,"\"")
                        tok[4]=x[2]")" 
                     }
                     if ((tok[2] != "LPM_TYPE") && (tok[2] != "LEVEL")&&\
                        (tok[2] != "VHDL") && (tok[2] != "LPM_POLARITY") &&\
			(substr(tok[2],1,4)!="LPM_"))
                     {
                        tok[2]="LPM_"tok[2]
                     }
                   }
		   if (tok[1] == "(port")
		   {
		      if ((substr(tok[2],1,3) == "SUM") && \
			  (lpm_type == "LPM_ADD_SUB"))
		      {
			 tok[2]="RESULT"substr(tok[2],4)
		      }
		      else
		      if ((substr(tok[2],1,6) == "SELECT") && \
			  (lpm_type == "LPM_MUX"))
		      {
			 tok[2]="SEL"substr(tok[2],7)
		      }
		      else
		      if ((substr(tok[2],1,7) == "PRODUCT") && \
			  (lpm_type == "LPM_MULT"))
		      {
			 tok[2]="RESULT"substr(tok[2],8)
		      }
		      else
		      if ((substr(tok[2],1,6) == "ENABLE") && \
			  (lpm_type == "LPM_COUNTER"))
		      {
			 tok[2]="CLK_EN"
			 add_cnt_en="true"
		      }
 		      else
		      if ((substr(tok[2],1,6) == "UPDOWN") && \
			  (lpm_type == "LPM_COUNTER"))
		      {
			 add_cnt_dir="false"
		      }
		      else
		      if ((substr(tok[2],1,7) == "MEMENAB") && \
                          (lpm_type == "LPM_RAM_DQ"))
		      {
	                 line[i++]=""
	                 line[i++]=""
	                 line[i]=""
	                 continue
		      }
		      else
		      if (substr(tok[2],1,4) == "DATA")
		      {
			 split(tok[2],x,"_")
			 if(x[2] != "")
                         {
     			    tok[2]=x[1]"x"x[2]
			 }
		      }
		   }
		   line[i]=tok[1]
                   j=2
                   while(tok[j] != "")
                   {
 		      line[i]=line[i]" "tok[j++]
 		   }
		   if (add_cnt_en == "true")
		   {
		      line[i]="(port CNT_EN\n(property LPM_POLARITY (string \"NORMAL\") (owner \"Viewlogic_Systems\"))\n(direction INPUT))\n"line[i]
		      add_cnt_en="fasle"
		   }
		   if ((tok[1]=="(property") && (tok[2] == "LPM_TYPE")\
		       &&(lpm_type=="LPM_COUNTER")&&(add_cnt_dir=="true"))
		   {
		      line[i]=line[i]"\n(property LPM_DIRECTION (string \"UP\") (owner \"Viewlogic Systems\"))"
		      add_cnt_dir="false"
		   }
               }
            }
            if (in_cell == "true")
	    {
               i=1
               while(i < line_count)
  	       {
                   print line[i++]
               }
            }
            cell_name=$2
	    inlpm="false"
            in_cell="true"
	    line_count=1
         }
	    
/\(property/ {  
            if ($2 == "LPM_TYPE")
            {
               inlpm="true"
               lpm_cell_name[lpm_cell_count++]=cell_name
	       split($4,x,"\"")
	       lpm_type=x[2]
	       if(x[2] == "LPM_COUNTER")
	       {
	          add_cnt_dir="true"	  
                  lpm_counter_name[lpm_counter_count++]=cell_name
	       }
	       else
	       if(x[2] == "LPM_ADD_SUB")
	       {
                  lpm_add_sub_name[lpm_add_sub_count++]=cell_name
	       }
            }
         } 
/\(instance/ {
   	   instance_name=$3 
	 }
/\(viewRef/ {
  	   split($4,x,")")
           for (i=1;i<lpm_cell_count;i++)
           {
              if( x[1] == lpm_cell_name[i])
              {
                 lpm_instance_id[lpm_instance++]=instance_name
 		 break
 	      }
	   } 
           for (i=1;i<lpm_counter_count;i++)
           {
              if( x[1] == lpm_counter_name[i])
              {
                 lpm_counter_id[lpm_counter_instance++]=instance_name
 		 break
 	      }
	   } 
 	 }
/\(portRef/ {
	    split($4,x,")")
            for(i=1;i<lpm_instance;i++)
            {
 	       if(x[1] == lpm_instance_id[i])
	       {
                 if (substr($2,1,4) == "DATA")
                 {
                    split($2,tmp,"_")
                    if (tmp[2] != "")
                    {
                       $2=tmp[1]"x"tmp[2]
                    }
                 }
		 else
		 if ((substr($2,1,7) == "PRODUCT"))
		 {
		    $2="RESULT"substr($2,8)
		 }
		 else
		 if ((substr($2,1,6) == "SELECT"))
		 {
		    $2="SEL"substr($2,7)
		 }
		 else
		 if ((substr($2,1,7) == "MEMENAB"))
		 {
	            print "line:",NR,"WARNING! MEMENAB port used."
		    exit 
		 }
		 else
		 if ((substr($2,1,3) == "SUM"))
		 {
		    $2="RESULT"substr($2,4)
		 }
		 else
		 if (substr($2,1,4) == "DATA")
		 {
		    split($2,x,"_")
		    if(x[2] != "")
                    {
     		       $2=x[1]"x"x[2]
		    }
		 }
                 break
               }
            }
            for(i=1;i<lpm_counter_instance;i++)
            {
 	       if(x[1] == lpm_counter_id[i])
	       {
                 if ($2 == "ENABLE")
                 {
		    $2="CLK_EN"
                 }
                 break
               }
            }
         }
         {
           if ( in_cell == "false" ) 
           {
              print $0
           }
           else
           {
              line[line_count++]=sprintf("%s",$0)
           }
         }
  END { 
        if (in_cell == "true")
        {
           for (i=1;i<line_count;i++)
           {
              print line[i]
           }
        }
      }
     
    ' $1 > tmpfile.$$
if grep "WARNING! MEMENAB port used." tmpfile.$$ 
then 
   echo "ERROR: No MEMENAB port for LPM_RAM_DQ is allowed in LPM 201. max2_edifo failed."
   rm $1
   rm tmpfile.$$
else
    sed -e "s/LPM_DISTWIDTHDIST/LPM_WIDTHDIST/g" \
        -e "s/LPM_AD_WIDTH/LPM_WIDTHAD/g" \
        -e "s/LPM_INWIDTH/LPM_WIDTHIN/g" \
        -e "s/LPM_OUTWIDTH/LPM_WIDTHOUT/g" \
        tmpfile.$$ > $1
    rm tmpfile.$$
fi
